package com.hjm.project.accounts.virtualAccount.controller;

import java.math.BigDecimal;
import java.util.List;

import com.hjm.common.utils.Utils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.accounts.virtualAccount.domain.VirtualAccount;
import com.hjm.project.accounts.virtualAccount.service.IVirtualAccountService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import javax.annotation.Resource;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 虚户Controller
 * 
 * @author maker-platform
 * @date 2022-03-16
 */
@Controller
@RequestMapping("/accounts/virtualAccount")
public class VirtualAccountController extends BaseController {
    private String prefix = "accounts/virtualAccount";

    @Resource
    private IVirtualAccountService virtualAccountService;

    @RequiresPermissions("accounts:virtualAccount:view")
    @GetMapping()
    public String virtualAccount() {
        return prefix + "/virtualAccount";
    }

    /**
     * 查询虚户列表
     */
    @RequiresPermissions("accounts:virtualAccount:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VirtualAccount virtualAccount) {
        startPage();
        List<VirtualAccount> list = virtualAccountService.selectVirtualAccountList(virtualAccount);
        return getDataTable(list);
    }

    /**
     * 导出虚户列表
     */
    @RequiresPermissions("accounts:virtualAccount:export")
    @Log(title = "虚户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualAccount virtualAccount) {
        List<VirtualAccount> list = virtualAccountService.selectVirtualAccountList(virtualAccount);
        ExcelUtil<VirtualAccount> util = new ExcelUtil<VirtualAccount>(VirtualAccount.class);
        return util.exportExcel(list, "virtualAccount");
    }

    /**
     * 新增虚户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存虚户
     */
    @RequiresPermissions("accounts:virtualAccount:add")
    @Log(title = "虚户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualAccount virtualAccount) {
        return toAjax(virtualAccountService.insertVirtualAccount(virtualAccount));
    }

    /**
     * 修改虚户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        VirtualAccount virtualAccount = virtualAccountService.selectVirtualAccountById(id);
        mmap.put("virtualAccount", virtualAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存虚户
     */
    @RequiresPermissions("accounts:virtualAccount:edit")
    @Log(title = "虚户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualAccount virtualAccount) {

        if (!Utils.isNotMoney(virtualAccount.getMoney())) {
            return error("请输入正确的金额");
        }
        if (new BigDecimal(virtualAccount.getMoney()).compareTo(new BigDecimal("0")) == 0){
            return error("提现金额不能小于0元");
        }
        return virtualAccountService.updateVirtualAccount(virtualAccount);
    }

    /**
     * 删除虚户
     */
    @RequiresPermissions("accounts:virtualAccount:remove")
    @Log(title = "虚户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(virtualAccountService.deleteVirtualAccountByIds(ids));
    }
}
